﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Text;
using System.IO;
using System.Globalization;
using SpLocalizer.Common;
using SpLocalizer.Common.DbApi;
using System.Text.RegularExpressions;

namespace SpLocalizer.ContentSourceProviders.XPath
{
    public class PublishingPageContent : XPathContent
    {

        private const string StartPattern = "<xml>";
        private const string EndPattern = "</xml>";

        public PublishingPageContent(string[] XPath) : base(XPath) { }


        public override void Bind(string XmlFilePath)
        {

            this.XmlDoc = new XmlDocument();
            this.XmlDoc.PreserveWhitespace = true;
            XmlSchema schema = new XmlSchema();
            schema.Namespaces.Add("mso", "http://www.contoso.com");
            schema.Namespaces.Add("msdt", "http://www.southridge.com");
            this.XmlDoc.Schemas.Add(schema);

            XmlDeclaration dec = this.XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            this.XmlDoc.AppendChild(dec);

          
            this.LoadPublishingPageXml(XmlFilePath);
            this.ContentAddress = XmlFilePath;
        }

        public override IEnumerable<BaseTranslationTask> BuildTranslationTasks(CultureInfo FromLanguage, CultureInfo ToLanguage)
        {
            IEnumerable<BaseTranslationTask> results = base.BuildTranslationTasks(FromLanguage, ToLanguage);

            foreach (BaseTranslationTask item in results)
            {
                item.Format = Common.Constants.TASK_FORMAT_HTML; // Content is in HTML format
            }

            return results;
        }

        private void LoadPublishingPageXml(string PageFilePath)
        {
            string PageContent = File.ReadAllText(PageFilePath);
            int StartIndex = PageContent.IndexOf(StartPattern);
            if (StartIndex < 0)
                return; // This is not a Publishsing page
            int EndIndex = PageContent.IndexOf(EndPattern) - StartIndex + EndPattern.Length;

          /*  int StartIndex = PageContent.IndexOf(StartPattern) + StartPattern.Length;
            int EndIndex = PageContent.IndexOf(EndPattern) - StartIndex -1;*/

            PageContent = PageContent.Substring(StartIndex, EndIndex);
           
            this.XmlDoc.LoadXml(PageContent);
        }

        public override void PerformExportDone()
        {
            if (string.IsNullOrEmpty(this.TaskLanguage))
                return;

            FileInfo fi = new FileInfo(this.ContentAddress);
            DirectoryInfo dir = fi.Directory; //.CreateSubdirectory(this.TaskLanguage);
            
            StringBuilder result = new StringBuilder();
            
            using (XmlWriter xw = XmlWriter.Create(result))
            {
                XmlDoc.WriteTo(xw);//XPathNav.WriteSubtree(xw);
            }


            string PageContent = File.ReadAllText(this.ContentAddress);
            
            int StartIndex = PageContent.IndexOf(StartPattern);
            int EndIndex = PageContent.IndexOf(EndPattern);
            result.Insert(0, PageContent.Substring(0,StartIndex));
            result.Append(PageContent.Substring(EndIndex + EndPattern.Length, PageContent.Length - EndIndex - EndPattern.Length));

            File.WriteAllText(dir.FullName + "\\" + fi.Name, result.ToString());
           

        }
    }
}
